//
//  FirendCircleDataSource.m
//  tcpoctest
//
//  Created by gump on 4/15/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "FirendCircleDataSource.h"
#import "LocalPathDataSource.h"
#import "FirCirBgImagedb.h"
#import "FirCirBgImageDataSource.h"
#import "FirCirNewsDataSource.h"

@implementation FirendCircleDataSource

-(id)init
{
    self = [super init];
    if (self != nil) {
        fircirShowArr = [[NSMutableArray alloc] init];
        pthread_mutex_init(&fircirMutexp, 0);
        self.sendStr = @"";
        sendIndex = FCG_ONE_SOUND;
        photoArr = [[NSMutableArray alloc] init];
        self.HudStr = @"";
        isendpiccount = 0;
        self.bgimage = nil;
        bSendBgImage = false;
        self.iPackupRow = 0;
        self.iPackupSection = 0;
        self.bPackup = false;
        self.newmail = @"";
        self.bShowBadge = false;
    }
    
    return self;
}

-(void)AddFirCirArrFirst:(NSString *)fs
{
    pthread_mutex_lock(&fircirMutexp);
    
    if (![fircirShowArr containsObject:fs]) {
        [fircirShowArr insertObject:fs atIndex:0];
    }
    
    pthread_mutex_unlock(&fircirMutexp);

}

-(void)AddFirCirArr:(NSString *)newsid
{
    pthread_mutex_lock(&fircirMutexp);
    
    [fircirShowArr addObject:newsid];
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(void)DelFirCirArrByID:(NSString *)strid
{
    pthread_mutex_lock(&fircirMutexp);
    
    [fircirShowArr removeObject:strid];
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(void)RemovAllArr
{
    pthread_mutex_lock(&fircirMutexp);
    
    [fircirShowArr removeAllObjects];
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(void)SetFCSendContent:(NSString *)strcontent
{
    pthread_mutex_lock(&fircirMutexp);
    
    self.sendStr = strcontent;
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(NSString*)GetFCSendContent
{
    NSString *strc = @"";
    
    pthread_mutex_lock(&fircirMutexp);
    
    strc = self.sendStr;
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return strc;
}

-(void)setFCSendIndex:(int)iIndex
{
    pthread_mutex_lock(&fircirMutexp);
    
    sendIndex = iIndex;
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(int)getFCSendIndex
{
    int iindex = FCG_ONE_TEXT;
    
    pthread_mutex_lock(&fircirMutexp);
    
    iindex = sendIndex;
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return iindex;
}

-(int)getShowArrCount
{
    int icount = 0;
    
    pthread_mutex_lock(&fircirMutexp);
    
    icount = [fircirShowArr count];
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return icount;
}

-(NSString *)getShowArrObjectByIndex:(int)row
{
    NSString *fs = @"";
    
    pthread_mutex_lock(&fircirMutexp);
    
    if (row > -1 && row < [fircirShowArr count]) {
        fs = [fircirShowArr objectAtIndex:row];
    }
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return fs;
}

-(int)getphotoArrCount
{
    int icount = 0;
    
    pthread_mutex_lock(&fircirMutexp);

    icount = [photoArr count];
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return icount;
}

-(UIImage *)getPhotoArrObjectIndex:(int)index
{
    UIImage *image = nil;
    
    pthread_mutex_lock(&fircirMutexp);
    
    if (index >= 0 && index < [photoArr count]) {
        image = [photoArr objectAtIndex:index];
    }
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return image;
}

-(void)RemovePhotoArrObjectByIndex:(int)index
{
    pthread_mutex_lock(&fircirMutexp);
    
    if (index >= 0 && index < [photoArr count]) {
        [photoArr removeObjectAtIndex:index];
    }
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(void)RemoveAllPhoto
{
    pthread_mutex_lock(&fircirMutexp);
    
    [photoArr removeAllObjects];
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(void)AddPhotoArrObject:(UIImage *)image
{
    pthread_mutex_lock(&fircirMutexp);
    
    [photoArr addObject:image];
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(void)SetHudString:(NSString *)str
{
    pthread_mutex_lock(&fircirMutexp);
    
    self.HudStr = str;
    
    pthread_mutex_unlock(&fircirMutexp);
 
}

-(NSString *)HubString
{
    NSString *str = @"";
    
    pthread_mutex_lock(&fircirMutexp);
    
    str = self.HudStr;
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return str;
}

-(void)SetSendPicCount:(int)icount
{
    pthread_mutex_lock(&fircirMutexp);
    
    isendpiccount = icount;
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(int)SendPicCount
{
    int icount = 0;
    
    pthread_mutex_lock(&fircirMutexp);
    
    icount = isendpiccount;
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return icount;
}

-(void)SetBGImage:(UIImage *)image
{
    pthread_mutex_lock(&fircirMutexp);
    
    self.bgimage = image;
    
    pthread_mutex_unlock(&fircirMutexp);
}

-(UIImage *)getBGImage
{
    UIImage *image = nil;
    
    pthread_mutex_lock(&fircirMutexp);
    
    image = self.bgimage;
    
    pthread_mutex_unlock(&fircirMutexp);
    
    return image;

}

-(void)SetISSetBGImage:(bool)bset
{
    pthread_mutex_lock(&fircirMutexp);
    
    bSendBgImage = bset;
    
    pthread_mutex_unlock(&fircirMutexp);

}

-(bool)IsSetBGImage
{
    bool bset = false;
    
    pthread_mutex_lock(&fircirMutexp);
    
    bset = bSendBgImage;
    
    pthread_mutex_unlock(&fircirMutexp);

    return bset;
}

-(void)SaveBGImageByMail:(NSString *)mail
{
        NSString *urlpath = @"";
        NSString *strname = getFileNameWithDate(FT_FCBGI);
        strname = [NSString stringWithFormat:@"%@%@",strname,mail];
        
        urlpath =[[[DataControl shareControl].localpathds FircirPath] stringByAppendingPathComponent:strname];
        
    NSData  *imagedata = UIImageJPEGRepresentation(self.bgimage, JPG_COMPRESS_RATE);
    
        if ([imagedata length] == 0) {
            return;
        }
        
        BOOL bsuccess = [imagedata writeToFile:urlpath atomically:YES];
        
        do {
            
            if (!bsuccess) {
                break;
            }
            
            [[DataBaseCtrol shareControl].fircirbgimagedatabase insertBgImageUrl:mail withURL:urlpath];
            
            
                
                    NSString *oldurl = [[DataControl shareControl].fcBgImageDS getPathByMail:mail];
                    if(oldurl != nil && ![oldurl isEqualToString:@""])
                    {
                        [[FileManager shareControl] deleteFile:oldurl];
                    }
                    
                    [[DataControl shareControl].fcBgImageDS AddImagePath:urlpath withMail:mail];
                 
           
            
        } while (false);

}

-(NSString *)greatFCcurTimeString
{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    
    [dateFormat setDateFormat:@"yyyy/MM/dd/HH/mm/ss"];
    
    NSString *curdatestr = [dateFormat stringFromDate:[NSDate date]];
    
    dateFormat = nil;
    
    return curdatestr;
}

-(void)sortShowArr
{
    pthread_mutex_lock(&fircirMutexp);
    
    [fircirShowArr sortUsingFunction:customSortFC context:nil];
    
    pthread_mutex_unlock(&fircirMutexp);
}

NSInteger customSortFC(id obj1, id obj2,void* context)
{
    NSString* obj_1 = (NSString*)obj1;
    NSString* obj_2 = (NSString*)obj2;
    
    NSComparisonResult res = (NSComparisonResult)NSOrderedSame;
    
    do {
        
        FriendStatus *fs1 = [[DataControl shareControl].fcnewsDS getNewsByID:obj_1];
        FriendStatus *fs2 = [[DataControl shareControl].fcnewsDS getNewsByID:obj_2];
        
        if (fs1 == nil || fs2 == nil) {
            break;
        }
        
        obj_1 = fs1.strtime;
        obj_2 = fs2.strtime;

        NSArray *array = [fs1.strtime componentsSeparatedByString:@"/"];
        NSArray *array1 = [fs2.strtime componentsSeparatedByString:@"/"];

        if ([array count] != 6 || [array1 count] != 6) {
            break;
        }

for(int i = 0 ; i < 6 ; ++i)
{
    int idate = [[array objectAtIndex:i] intValue];
    int idate1 = [[array1 objectAtIndex:i] intValue];
    
    if(idate > idate1)
    {
        res = (NSComparisonResult)NSOrderedAscending;
        break;
    }
    else if(idate < idate1)
    {
        res = (NSComparisonResult)NSOrderedDescending;
        break;
    }
}


        

    } while (false);

    return res;
}

@end
